import { request } from '@umijs/max';

/** 获取当前的用户 GET /api/currentUser */
export async function currentUser(options?: { [key: string]: any }) {
  return request<{
    data: API.CurrentUser;
  }>('/api/currentUser', {
    method: 'GET',
    ...(options || {}),
  });
}

/** 退出登录接口 POST /api/login/outLogin */
export async function outLogin(options?: { [key: string]: any }) {
  return request<Record<string, any>>('/api/login/outLogin', {
    method: 'POST',
    ...(options || {}),
  });
}

/** 登录接口 POST /api/login/account */
export async function login(body: API.LoginParams, options?: { [key: string]: any }) {
  return request<API.LoginResult>('/api/login/account', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: body,
    ...(options || {}),
  });
}

/** 此处后端没有提供注释 GET /api/notices */
export async function getNotices(options?: { [key: string]: any }) {
  return request<API.NoticeIconList>('/api/notices', {
    method: 'GET',
    ...(options || {}),
  });
}

/** 获取规则列表 GET /api/rule */
export async function rule(
  params: {
    // query
    /** 当前的页码 */
    current?: number;
    /** 页面的容量 */
    pageSize?: number;
  },
  options?: { [key: string]: any },
) {
  return request<API.RuleList>('/api/rule', {
    method: 'GET',
    params: {
      ...params,
    },
    ...(options || {}),
  });
}

/** 更新规则 PUT /api/rule */
export async function updateRule(body: any, options?: { [key: string]: any }) {
  return request<API.RuleListItem>('/api/rule', {
    method: 'POST',
    data: {
      ...body,
      method: 'update'
    },
    ...(options || {}),
  });
}

/** 新建规则 POST /api/rule */
export async function addRule(body: any, options?: { [key: string]: any }) {
  return request<API.RuleListItem>('/api/rule', {
    method: 'POST',
    data: {//请求体 body name desc method=post
      ...body,
      method: 'post'
    },
    ...(options || {}),
  });
}

/** 删除规则 DELETE /api/rule */
export async function removeRule(body: any, options?: { [key: string]: any }) {
  return request<Record<string, any>>('/api/rule', {
    method: 'POST',
    data: {
      ...body,
      method: 'delete'
    },
    ...(options || {}),
  });
}



/** 获取用户列表 GET /api/user */
//(params?: {pageSize,current},sort,filter) => {data,success,total}
export async function user(
  params: any,
  sort,
  filter
) {
  console.log('sort',sort)
  console.log('filter',filter)
  if(sort && Object.keys(sort).length>0){
    params.sorter = JSON.stringify(sort);
  }
  if(filter && Object.keys(filter).length>0){
    params.filter = JSON.stringify(filter);
  }
  return request('/api/user', {
    method: 'GET',
    params
  });
}

/** 更新用户 PUT /api/user */
export async function updateUser(body: any) {
  return request('/api/user', {
    method: 'PUT',
    data: body
  });
}

/** 新建用户 POST /api/user */
export async function addUser(body: any) {
  return request('/api/user', {
    method: 'POST',
    data: body
  });
}

/** 删除用户 DELETE /api/user */
export async function removeUser(body: any) {
  return request<Record<string, any>>('/api/user', {
    method: 'DELETE',
    data: body
  });
}
